/*************************************************************************
	> File Name: oj-271.cpp
	> Author: Wang Bo
	> Mail: 1276106221@qq.com
	> Created Time: 2021年01月10日 星期日 15时48分00秒
 ************************************************************************/
#include <iostream>
#include <queue>
using namespace std;
//单调队列的裸题
#define MAX_N 300000
int q[MAX_N + 5], head, tail;
int val[MAX_N + 5];

int main() {
    int n, k; 
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        cin >> val[i];
    }
    for (int i = 1; i <= n; ++i) { /*窗口最小值*/
        while (tail - head && val[q[tail - 1]] > val[i]) --tail;
        q[tail++] = i;
        if (q[head] <= i - k) head++;
        if (i >= k) {
            i > k && cout << " ";
            cout << val[q[head]];
        }
    }
    cout << endl;
    head = tail = 0;
    for (int i = 1; i <= n; ++i) { /*窗口最大值*/
        while (tail - head && val[q[tail - 1]] < val[i]) --tail;
        q[tail++] = i;
        if (q[head] <= i - k) head++;
        if (i >= k) {
            i > k && cout << " ";
            cout << val[q[head]];
        }
    }
    cout << endl;
    return 0;
}
